

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime


file_handle=open('x.txt',mode='w')
out =("sss","ddddd","ddddd","ddddd","ddddd","dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffddddddd","\n")
print(str(out))
file_handle.write(",".join(out))
file_handle.write(",".join(out))
file_handle.close()
print('111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',end=' ',flush= True)


class test_s:
    def __init__(self):
        self.id = [];
items = test_s()

items.id.append(1)
items.id.append(1)
items.id.append(1)
items.id.append(1)
items.id.append(1)
items.id.append(1)
class group_data:
    def __init__(self):
        self.id = 0;
        self.time = "";
        self.bid_index  = 0;
        self.max = 0
        self.count = 0

        
group_dict ={}
pre = group_data()
X, Y = [], []
stamp1 = "09:30:0.0" 
stamp2 = "14:56:59.0" 
print (datetime.strptime(stamp1, '%H:%M:%S.%f')- datetime.strptime(stamp2, '%H:%M:%S.%f'))
i = 0
time_idx = -1
group_idx = -1
bid_idx = -1
#for line in open('test', 'r'):
#  values =  line.split(',')
#  if values[0] =="td SSE":
#      time_idx = 2
#      group_idx = 5
#      bid_idx = 11
#  else:
#      time_idx = 2
#      group_idx = 5
#      bid_idx = 9
#  curr = group_data()
#  curr.id = values[group_idx]
#  curr.time = values[time_idx]
#  curr.bid_index = values[bid_idx]
#  if (datetime.strptime(curr.time, '%H:%M:%S.%f')<datetime.strptime("09:30:00.000", '%H:%M:%S.%f')  or  datetime.strptime(curr.time, '%H:%M:%S.%f')> datetime.strptime("14:27:00.000", '%H:%M:%S.%f') ):
#      print(curr.time)
#      continue
#
#  if (curr.id in group_dict):
#      pre = group_dict[curr.id]
#      if (int(curr.bid_index) - int(pre.bid_index) < 0 ):
#          diff = -(int(curr.bid_index) - int(pre.bid_index))
#          if (diff > pre.max):
#              pre.max = diff
#          stamp_pre =pre.time 
#          stamp_curr =curr.time 
#          diff_time = (datetime.strptime(stamp_pre, '%H:%M:%S.%f') - datetime.strptime(stamp_curr, '%H:%M:%S.%f')).total_seconds() 
#          pre.count = pre.count+1
#          print("group,",curr.id ,",back,",diff,",diff_time(s),",diff_time,",max,",pre.max,",back count,",pre.count,",curr_bid,", curr.bid_index  )
#          pre.bid_index = curr.bid_index
#          pre.time = curr.time
#      else:
#          pre.bid_index = curr.bid_index
#          pre.time = curr.time
#         
#  else:
#       group_dict[curr.id] = curr

class group_s:
    def __init__(self,b,e):
        self.id = 0;
        self.time = "";
        self.bid_index  = 0;
        self.max_back = 0
        self.count = 0
        self.begin = b
        self.end = e
        self.back= []
        self.line= 0
        self.mean = 0
        self.std = 0
        self.median = 0
        self.min = 0
        self.max = 0
        self.percent = 0


Z=[] 
for line in open('time4.log', 'r'):
    #line.rstrip('\n')
    values = line.split(',')
    Z.append(str(values[17].replace('\n','')))


from matplotlib import pyplot as plt 
from matplotlib import dates as mdates

conv_time = [datetime.strptime(i, "%H:%M:%S.%f") for i in Z]
#define bin number
bin_nr = 8
fig, ax = plt.subplots(1,1)
#create histogram, get bin position for label
_counts, bins, _patches = ax.hist(conv_time, bins = bin_nr)
#set xticks at bin edges
plt.xticks(bins)
#reformat bin label into format hour:minute
ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
plt.xlabel('Time Range 9:30:00 --15:00:00')
plt.ylabel('Back Count')
plt.show()








print(np.median(Y))


plt.xlabel('time 9:30 --15:00')
plt.ylabel('time  (s)')

#plt.plot(X, Y)
#plt.show()
bins=np.arange(0,25,1)
plt.hist(Y,bins, density=False, facecolor='g', alpha=0.75)
#plt.xlim(0, 40000)
plt.ylabel('message count')
plt.xlabel('time delay(s)')

plt.show()